using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Timer;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using System;
namespace MicroMsg.Scene.Voice
{
	public class NetSceneUploadVoice : NetSceneBaseEx<UploadVoiceRequest, UploadVoiceResponse, UploadVoiceRequest.Builder>
	{
		private const string TAG = "NetSceneUploadVoice";
		private UploadVoiceContext mVoiceContext;
		private TimerObject mTimerObject;
		public event onSceneFinishedDelegate mOnSceneFinished;
		private bool doSceneEx(byte[] toSend)
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(19);
			this.mBuilder.FromUserName = AccountMgr.getCurAccount().strUsrName;
			this.mBuilder.ToUserName = this.mVoiceContext.mUserName;
			this.mBuilder.Offset = (uint)this.mVoiceContext.mNetOffset;
			this.mBuilder.Length = (uint)toSend.Length;
			this.mBuilder.ClientMsgId = this.mVoiceContext.mClientMsgId;
			this.mBuilder.MsgId = (uint)this.mVoiceContext.mMsgSrvId;
			this.mBuilder.VoiceLength = (uint)this.mVoiceContext.mVoiceTimeLength;
			this.mBuilder.Data = Util.toSKBuffer(toSend);
			uint endFlag = 0u;
			if (this.mVoiceContext.isEncodeAmrEnd() && this.mVoiceContext.mNetOffset + toSend.Length >= this.mVoiceContext.mOutputLength)
			{
				endFlag = 1u;
			}
			this.mBuilder.EndFlag = endFlag;
			if (this.mVoiceContext.mIsCancelled)
			{
				this.mBuilder.CancelFlag = 1u;
				this.mBuilder.Data = Util.toSKBuffer("");
				this.mBuilder.Length = 0u;
			}
			this.mSessionPack.mCmdID = 19;
			base.endBuilder();
			return true;
		}
		protected override void onFailed(UploadVoiceRequest request, UploadVoiceResponse response)
		{
			Log.smethod_1("NetSceneUploadVoice", "upload Voice Block  failed ");
			if (this.mVoiceContext == null)
			{
				return;
			}
			this.mVoiceContext.mStatus = 4;
			this.dispatchVoiceHandler();
		}
		protected override void onSuccess(UploadVoiceRequest request, UploadVoiceResponse response)
		{
			if (this.mVoiceContext == null)
			{
				return;
			}
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret == RetConst.MM_OK)
			{
				Log.smethod_5("NetSceneUploadVoice", "upload Voice Block success ");
				this.mVoiceContext.mNetOffset = this.mVoiceContext.mNetOffset + this.mVoiceContext.mSendingLength;
				this.mVoiceContext.mMsgSrvId = (int)response.MsgId;
				this.mVoiceContext.mSendingLength = 0;
				this.mVoiceContext.mStatus = 1;
			}
			else
			{
				Log.smethod_1("NetSceneUploadVoice", "upload Voice Block failed, ret =  " + ret);
				this.mVoiceContext.mStatus = 4;
				if (ret == RetConst.MM_ERR_NEED_QQPWD)
				{
					EventCenter.postEvent(EventConst.ON_NETSCENE_UPLOADVOICE_ERR, new UploadVoiceEventArgs(this.mVoiceContext.mChatMsg, ret), null);
				}
			}
			this.dispatchVoiceHandler();
		}
		public void doSceneFinished(int finishType)
		{
			this.mTimerObject.stop();
			this.mTimerObject = null;
			if (finishType != 0)
			{
				this.mVoiceContext.onFinished(finishType);
				if (this.mOnSceneFinished != null)
				{
					this.mOnSceneFinished(this.mVoiceContext);
				}
			}
			this.mVoiceContext.mScene = null;
			this.mVoiceContext = null;
		}
		public static void onTimerHandler(object sender, EventArgs e)
		{
			NetSceneUploadVoice netSceneUploadVoice = TimerEventArgs.getObject(e) as NetSceneUploadVoice;
			if (netSceneUploadVoice != null && netSceneUploadVoice.mVoiceContext != null)
			{
				netSceneUploadVoice.mVoiceContext.printfInfo();
				netSceneUploadVoice.dispatchVoiceHandler();
				return;
			}
			Log.smethod_1("NetSceneUploadVoice", "onTimerHandler, invalid timer args");
		}
		public void dispatchVoiceHandler()
		{
			UploadVoiceContext uploadVoiceContext = this.mVoiceContext;
			if (uploadVoiceContext == null)
			{
				return;
			}
			if (uploadVoiceContext.isFinished())
			{
				Log.smethod_1("NetSceneUploadVoice", "completed already, close netscene.. ");
				this.doSceneFinished(0);
				return;
			}
			if (uploadVoiceContext.isInvalidShortVoice() && uploadVoiceContext.isRecordEnd())
			{
				uploadVoiceContext.mStatus = 4;
				Log.smethod_1("NetSceneUploadVoice", "complete with short voice , ignored.. ");
				this.doSceneFinished(3);
				return;
			}
			if (uploadVoiceContext.isSaveCompleted() && uploadVoiceContext.isSendCompleted())
			{
				uploadVoiceContext.mStatus = 5;
				this.doSceneFinished(1);
				return;
			}
			if (uploadVoiceContext.isSaveCompleted() && uploadVoiceContext.mStatus == 4)
			{
				Log.smethod_1("NetSceneUploadVoice", "save completed, but send error. ");
				this.doSceneFinished(2);
				return;
			}
			this.checkRecorderSaver();
			this.checkNetScene();
		}
		public void checkRecorderSaver()
		{
			if (this.mVoiceContext.isSaveCompleted())
			{
				return;
			}
			this.mVoiceContext.checkRecorderSaver();
		}
		public void checkNetScene()
		{
			if (this.mVoiceContext.mStatus != 1)
			{
				return;
			}
			if (this.mVoiceContext.isSendCompleted())
			{
				return;
			}
			if (this.mVoiceContext.isInvalidShortVoice())
			{
				return;
			}
			byte[] bufferFromHead = this.mVoiceContext.getBufferFromHead();
			if (bufferFromHead == null)
			{
				return;
			}
			this.mVoiceContext.mStatus = 2;
			this.doSceneEx(bufferFromHead);
		}
		public void doScene(UploadVoiceContext voiceContext)
		{
			if (voiceContext.mStatus != 0 && voiceContext.mStatus != 4)
			{
				Log.smethod_1("NetSceneUploadVoice", "doScene status error, status = " + voiceContext.mStatus);
				return;
			}
			voiceContext.mStatus = 1;
			this.mVoiceContext = voiceContext;
			this.mVoiceContext.mScene = this;
			this.mTimerObject = TimerService.addTimer(1, new EventHandler(NetSceneUploadVoice.onTimerHandler), 0, -1, new TimerEventArgs(this));
			this.mTimerObject.start();
		}
	}
}
